<?php

class MerchantUserForm extends BaseForm {
  const FORM_NAME           = 'frm';

  const FIELD_MERCHANT_ID   = 'merchant_id';
  const FIELD_FIRST_NAME    = 'first_name';
  const FIELD_LAST_NAME     = 'last_name';
  const FIELD_EMAIL         = 'email';
  const FIELD_TYPE          = 'type';
  const FIELD_STATUS        = 'status';

  const SORT_FIRST_NAME     = 'FIRST_NAME';
  const SORT_LAST_NAME      = 'LAST_NAME';
  const SORT_CREATED_AT     = 'CREATED_AT desc';

  private $user             = null;

  public function setUser(USER $user) {
    $this->user = $user;
  }

  public function configure() {
    $arrStatusList = StringUtil::getStatusArray();
    $arrTypeList = StringUtil::getAccountTypeArray();

    $widget = array(
            self::FIELD_MERCHANT_ID   => new sfWidgetFormInputHidden(array(), array()),
            self::FIELD_FIRST_NAME    => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
            self::FIELD_LAST_NAME     => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
            self::FIELD_EMAIL         => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
            self::FIELD_TYPE          => new sfWidgetFormSelect(array('choices' => $arrTypeList), array('multiple' => false, 'expand' => true)),
            self::FIELD_STATUS        => new sfWidgetFormSelect(array('choices' => $arrStatusList), array('multiple' => false, 'expand' => true))
    );

    $labels = array(
            self::FIELD_MERCHANT_ID   => 'Merchant<req />',
            self::FIELD_FIRST_NAME    => 'First Name<req />',
            self::FIELD_LAST_NAME     => 'Last Name<req />',
            self::FIELD_EMAIL         => 'Email<req />',
            self::FIELD_TYPE          => 'Type<req />',
            self::FIELD_STATUS        => 'Status<req />'
    );

    $this->setWidgets($widget);
    $this->widgetSchema->setLabels($labels);

    $validators = array(
            self::FIELD_FIRST_NAME    => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_FIRST_NAME])))),
            self::FIELD_LAST_NAME     => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_LAST_NAME])))),
            self::FIELD_EMAIL         => new sfValidatorEmail(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_EMAIL])), 'invalid' => LangUtil::getText('error_invalid', array($labels[self::FIELD_EMAIL])))),
            self::FIELD_TYPE          => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_TYPE])))),
            self::FIELD_STATUS        => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_STATUS]))))
    );

    $this->setValidators($validators);
    $this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'postValidator'))));

    $this->widgetSchema->setNameFormat(self::FORM_NAME . '[%s]');

    $this->validatorSchema->setOption('allow_extra_fields', true);
    $this->validatorSchema->setOption('filter_extra_fields', false);
  }

  public function postValidator($validator, $values) {
    if ($this->user->isNew()) {
      if (trim($values[self::FIELD_EMAIL]) != '') {
        $user = USERTable::getInstance()->findByUsername(trim($values[self::FIELD_EMAIL]));

        $error = new sfValidatorError($validator, 'Email already exists.');
        if (!is_null($user) && is_object($user) && ($user instanceof USER)) {
          throw new sfValidatorErrorSchema($validator, array(self::FIELD_EMAIL => $error));
        }
      }
    }

    return $values;
  }

  public function populateData(USER $user) {
    $this->setDefault(self::FIELD_MERCHANT_ID, $user->getMerchantId());
    $this->setDefault(self::FIELD_FIRST_NAME, $user->getFirstName());
    $this->setDefault(self::FIELD_LAST_NAME, $user->getLastName());
    $this->setDefault(self::FIELD_EMAIL, $user->getUsername());
    $this->setDefault(self::FIELD_TYPE, $user->getUserGroupId());
    $this->setDefault(self::FIELD_STATUS, $user->getStatus());
  }
}
?>